package com.prs.crm.action.stock;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.context.Constrains;
import com.prs.crm.domain.stock.ClassAttribute;
import com.prs.crm.domain.stock.ProductClass;
import com.prs.crm.domain.sys.ChooseItem;
import com.prs.crm.service.stock.ProductClassService;

public class ProductClassAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private Integer pageNo;

	private Integer pageSize;

	private ProductClassService productClassService;

	private PaginationSupport pagination;

	private ProductClass productClass;

	private ProductClass parentClass;

	private Collection<TreeNode> nodes;

	private ClassAttribute attr;

	private Integer[] attrIds;

	private ChooseItem item;

	private String itemValue;

	public ProductClassService getProductClassService() {
		return productClassService;
	}

	public ProductClass getProductClass() {
		return productClass;
	}

	public ProductClass getParentClass() {
		return parentClass;
	}

	public void setParentClass(ProductClass parentClass) {
		this.parentClass = parentClass;
	}

	public void setProductClass(ProductClass productClass) {
		this.productClass = productClass;
	}

	public void setProductClassService(ProductClassService productClassService) {
		this.productClassService = productClassService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setNodes(Collection<TreeNode> nodes) {
		this.nodes = nodes;
	}

	public Collection<TreeNode> getNodes() {
		return nodes;
	}

	public void setAttr(ClassAttribute attr) {
		this.attr = attr;
	}

	public ClassAttribute getAttr() {
		return attr;
	}

	public void setAttrIds(Integer[] attrIds) {
		this.attrIds = attrIds;
	}

	public Integer[] getAttrIds() {
		return attrIds;
	}

	public void setItem(ChooseItem item) {
		this.item = item;
	}

	public ChooseItem getItem() {
		return item;
	}

	public String getItemValue() {
		return itemValue;
	}

	public void setItemValue(String itemValue) {
		this.itemValue = itemValue;
	}

	@Action("setProductClass")
	public String setProductClass() {
		return SUCCESS;
	}

	@Action("listProductClass")
	public String listProductClass() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				ProductClass.class);
		this.setPagination(this.getProductClassService().listByPage(criteria,
				getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}

	@Action(value = "loadProductClass", results = { @Result(name = "success", type = "json", params = {
			"root", "productClass", "excludeProperties", "childClasses" }) })
	public String loadProductClass() {
		return SUCCESS;
	}

	@Action(value = "loadProductClassTree", results = { @Result(name = "success", type = "json", params = {
			"root", "nodes" }) })
	public String loadProductClassTree() {
		nodes = new ArrayList<TreeNode>();
		Collection<ProductClass> clses = this.getProductClassService()
				.findRootClasses();
		for (ProductClass pls : clses) {
			TreeNode node = new TreeNode();
			node.setId(pls.getId().toString());
			node.setText(pls.getName());
			nodes.add(node);
			addChildNode(pls, node);
		}
		return SUCCESS;
	}

	private void addChildNode(ProductClass parent, TreeNode parentNode) {
		if (parent.getChildClasses() != null) {
			for (ProductClass pls : parent.getChildClasses()) {
				TreeNode node = new TreeNode();
				node.setId(pls.getId().toString());
				node.setText(pls.getName());
				parentNode.addChildNode(node);
				addChildNode(pls, node);
			}
		}
	}
	@Action(value = "saveProductClass1", results = {
			@Result(name = "success", type = "redirect", location = "listProductClass"),
			@Result(name = "error", type = "chain", location = "editProductClass")})
	public String saveProductClass1() {
		if(productClass.getName()==null||productClass.getName().equals("")){
			this.addActionError("名称不能为空");
			return ERROR;
		}
		for(ProductClass class1:this.getProductClassService().getProductClasses()){
			if(class1.getName().trim().equals(productClass.getName().trim())){
				this.addActionError("名称已经存在");
				return ERROR;
			}
		}
		this.getProductClassService().save(productClass);
		return SUCCESS;
	}
	@Action(value = "saveProductClass", results = { @Result(name = "success", type = "json", params = {
			"root", "productClass", "excludeProperties", "childs,parent" }) })
	public String saveProductClass() {
		try {
			if (productClass.getId() == null)
				productClass.setParent(parentClass);
			this.getProductClassService().save(productClass);
		} catch (Exception e) {
			this.setProductClass(null);
		}
		return SUCCESS;
	}

	@Action("editProductClass")
	public String editProductClass() {
		return SUCCESS;
	}

	@Action(value = "removeProductClass", results = { @Result(name = "success", type = "json", params = {
			"root", "prompt" }) })
	public String removeProductClass() {
		try {
			this.getProductClassService().remove(productClass);
			this.setPrompt("success");
		} catch (Exception e) {
			this.setPrompt("error");
		}
		return SUCCESS;
	}

	@Action("listClassAttrs")
	public String listClassAttrs() {
		return SUCCESS;
	}

	@Action("editClassAttr")
	public String editClassAttr() {
		if (attr != null && productClass == null)
			this.setProductClass(attr.getProductClass());
		return SUCCESS;
	}

	@Action(value = "saveClassAttr", results = {
			@Result(name = "success", type = "redirect", location = "listClassAttrs?productClass=${productClass.id}"),
			@Result(name = "input", type = "redirect", location = "editClassAttr?attr=${attr.id}") })
	public String saveClassAttr() {
		String result = SUCCESS;
		if (attr.getId() == null
				&& attr.getUi().getUiType() == Constrains.FIELDUI_CHOOSE)
			result = INPUT;

		this.getProductClassService().saveAttribute(productClass, attr);
		return result;
	}

	@Action(value = "removeClassAttr", results = { @Result(name = "success", type = "redirect", location = "listClassAttrs?productClass=${productClass.id}") })
	public String removeClassAttr() {
		this.getProductClassService().removeAttributes(attrIds);
		return SUCCESS;
	}

	@Action(value = "saveAttrChooseItem", results = { @Result(name = "success", type = "redirect", location = "editClassAttr?attr=${attr.id}") })
	public String saveAttrChooseItem() {
		this.getProductClassService().saveAttrChooseItem(attr, itemValue);
		return SUCCESS;
	}

	@Action(value = "removeAttrChooseItem", results = { @Result(name = "success", type = "redirect", location = "editClassAttr?attr=${attr.id}") })
	public String removeAttrChooseItem() {
		this.getProductClassService().removeAttrItem(attr, item);
		return SUCCESS;
	}
}
